<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2022/5/18
 * Time: 11:24
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

namespace app\api\controller\v1;

use app\common\lib\Show;
use app\common\lib\Status;
use app\common\model\PointGoods;
use app\common\model\PointReceive;
use app\common\model\WeixinExpress;
use Htlove\Str;
use think\App;

class Point extends Base
{
    protected $model;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new PointGoods();
    }

    /**
     * @return array|\think\response\Json
     * @throws \think\Exception
     */
    public function getPointByList()
    {
        $row = $this->model->getBaseByList(['status' => 1]);
        return Show::json_show(Status::mini_normal, "ok", $row);
    }

    /**
     * 详情
     * @return array|\think\response\Json
     * @throws \think\Exception
     */
    public function getPointDetail()
    {
        $id = $this->request->param('id', 0, 'intval');
        if ($id == 0) {
            return Show::json_show(Status::mini_error, "类型错误");
        }
        $row = $this->model->getBaseById($id);
        if (empty($row)) {
            return Show::json_show(Status::mini_error, "商品信息不存在");
        }
        $row['description'] = htmlspecialchars_decode($row['description']);
        $row['images'] = explode('|', $row['images']);
        return Show::json_show(Status::mini_normal, "ok", $row);
    }

    public function setPointByData()
    {
        $id = $this->request->param('id', 0, 'intval');
        if ($id == 0) {
            return Show::json_show(Status::mini_error, "内部异常");
        }
        $row = $this->model->getBaseById($id);
        if (empty($row)) {
            return Show::json_show(Status::mini_error, "商品信息不存在");
        }
        if ($row['stock'] == 0) {
            return Show::json_show(Status::mini_error, "商品库存不足");
        }
        $obj = new PointReceive();
        $surplus = $obj->getUserIntegralSurplus($this->m_openid);
        if ($row['point'] > $surplus) {
            return Show::json_show(Status::mini_error, "剩余积分不足");
        }
        //消费积分
        $this->model->startTrans();
        try {
            $this->model->where('id', $row['id'])->inc('stock', 5)->update();  //商品库存-1
            if ($row['type'] == 1) {
                //进行下单商品
                $insert_order = [
                    "order_sn" => Str::instance()->RandNumberStr(),
                    "mode_type" => 3,
                    "openid" => $this->m_openid,
                    "shippingtype" => 1,

                ];
            } else if ($row['type'] == 2) {
                //
                $insert_express = [
                    "num" => $row['point'],
                    "openid" => $this->m_openid,
                    "source" => 2,
                    "status" => 1
                ];
                (new WeixinExpress())->insert($insert_express);
            }
            //减少积分
            $insert_data = [
                "source_type"=>2,
                "source_id"=>$row['id'],
                "number"=>"-".$row['point'],
                "openid"=>$this->m_openid
            ];
            $obj->insert($insert_data);
            $this->model->commit();
        } catch (\Exception $e) {
            $this->model->rollback();
            return Show::json_show(Status::mini_error, $e->getMessage());
        }
        return Show::json_show(Status::mini_normal,"兑换成功");
    }
}